<?php

require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';
require_once 'modules/language/models/Lang.php';
class content_IndexController extends Nine_Controller_Action
{
	public function loadImageAction()
	{
		$this->setLayout("default");
		$objContent = new Models_Content();
        $objLang    = new Models_Lang();
        $objCat     = new Models_ContentCategory();
        $gid = $this->_getParam('id',false);
        $defaultImage = $objContent->setAllLanguages(true)->getContentById($gid);
//        echo "<pre>";print_r($allContent);die;
		if(null != @$defaultImage['images']) {
            		$defaultImage['main_image'] = explode('||', trim($defaultImage['images'],'|| '));
            		
            		$flag = 0;
            		$class ="";
        			foreach ($defaultImage['main_image'] as &$item){
		            		if(null != $item) {
		            		if($flag == 0){$class = "cubeRandom";}
		            		if($flag == 1){$class = "block";}
		            		if($flag == 2){$class = "cubeStop";}
		            		if($flag == 3){$class = "cubeHide";$flag = 0;}
	            			$tmp = @explode(":", trim($item,': '));
	            			$item = array(
				            			'path' => Nine_Function::getThumbImage(@$tmp[0], 695, 365,null,true),
				            			'title' => @$tmp[1],
	            						'class' => $class
				            			);
	            		}
	            		$flag++;
        			}
        			unset($item);
        			
            	}
//            	echo "<pre>";print_r($defaultImage);die;
        $this->view->image = $defaultImage;
        
	}
	
	public function hopdongAction()
	{
		$this->setLayout("hopdong");
		$this->view->menuId = 'contract';
		$objContent = new Models_Content();
        $objLang    = new Models_Lang();
        $objCat     = new Models_ContentCategory();
        
		$gid = $this->_getParam('id',false);
		$condition = array();
        $condition['content_category_gid'] = $gid;
        $allContent = $objContent->setAllLanguages(true)->getAllContent($condition,"sorting");
//        echo "<pre>";print_r($gid);die;
		$catId = current($allContent);
		$this->view->idMenu = $catId['content_category_gid'];
        $this->view->allContent = $allContent;
        $this->view->count = count($allContent);
        
		
	}
	
	public function gioithieuAction()
	{
		$this->setLayout("gioithieu");
		$this->view->menuId = 'about';
		$objContent = new Models_Content();
        $objLang    = new Models_Lang();
        $objCat     = new Models_ContentCategory();
        
		$gid = $this->_getParam('id',false);
		$this->view->idMenu = $gid;
        $allContent = $objContent->setAllLanguages(true)->getContentById($gid);
//        echo "<pre>";print_r($gid);die;
        $this->view->allContent = $allContent;
        
		
	}
	
	
	public function indexAction()
	{
		
		$objContent = new Models_Content();
        $objContentCategory = new Models_ContentCategory();
        
        /**
         * Get category
         */
        $categoryGid = $this->_getParam('cid',false);
        $alias = $this->_getParam('alias',false);
        if (false == $categoryGid) {
        	if (false == $alias) {
            	$this->_redirectToNotFoundPage();
        	}
        	else {
        		$cat = @reset($objContentCategory->getByColumns(array('alias=?' => $alias))->toArray());
        		$this->_request->setParam('cid', $cat['content_category_gid']);
        	}
        
        }
        else {
        	$cat = $objContentCategory->getCategoryWithParent($categoryGid);
        }
        $condition = array();
        if ('service' == $cat['template']){
            $allContents = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                        array('sorting ASC'));
            $i = 0;    
            foreach ($allContents as &$item) {
            	$i++;
            	$item['num'] = $i;
            	$item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
            }
            unset($item);

            $this->view->service = $allContents;
            $this->view->menuId = 'service';
        }
		else if ('reference' == $cat['template']){
			
			/**
			 * Get all references
			 */
		    $condition = array();
		    $numRowPerPage = Nine_Registry::getConfig("referNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allReferences = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                         array('sorting ASC','content_id DESC'),
                                                                             $numRowPerPage,($currentPage - 1) * $numRowPerPage);
            foreach ($allReferences as &$item){
            	if(null != @$item['images']) {
            		$item['main_image'] = @reset(explode('||', trim($item['images'],'|| ')));
            		if(null != $item['main_image']) {
            			$tmp = @explode(":", trim($item['main_image'],': '));
            			$item['main_image'] = array(
			            			'path' => Nine_Function::getThumbImage(@$tmp[0], 300, 200,null,true),
			            			'title' => @$tmp[1]
			            			);
            		}
            	}
		        $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
            }
            unset($item);
            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid']));
            /**
             * Assign to view
             */
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           
            $this->view->allReferences= array_chunk($allReferences,3);
            $this->view->menuId = "reference";                               
        }
		else if ('blogs' == $cat['template']){
            $condition = array();
            $numRowPerPage = Nine_Registry::getConfig("blogNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allBlogs = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                         array('sorting ASC','content_id DESC'),
                                                                             $numRowPerPage,($currentPage - 1) * $numRowPerPage);
            foreach ($allBlogs as &$item){
                $item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])),900);
//                $tmp = explode('||', $item['images']);
//                $item['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 192, 135,null,true);
            	if(null != @$item['images']) {
            		$item['main_image'] = @reset(explode('||', trim($item['images'],'|| ')));
            		if(null != $item['main_image']) {
            			$tmp = @explode(":", trim($item['main_image'],': '));
            			$item['main_image'] = array(
			            			'path' => Nine_Function::getThumbImage(@$tmp[0], 284, 189,null,true),
			            			'title' => @$tmp[1]
			            			);
            		}
            	}
                
                $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
                $item['created_date'] = date('F/d/Y',$item['created_date']);
                $item['date'] = explode('/', $item['created_date']);
            }
            unset($item);
            
            
            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid']));
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           

            $this->view->blog= $allBlogs;
        }
	   	else if ('partner' == $cat['template']){
            $condition = array();
            $numRowPerPage = Nine_Registry::getConfig("partnerNumberRowPerPage"); 
            $currentPage = $this->_getParam("page",1);
            $allPartner = $objContent->getAllEnabledContentByCategory($cat['content_category_gid'], $condition, 
                                                                         array('sorting ASC','content_id DESC'),
                                                                             $numRowPerPage,($currentPage - 1) * $numRowPerPage);
                                                                        
             foreach ($allPartner as &$item){
                $item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])),30);
                $tmp = explode('||', $item['images']);
                $item['images_main'] = Nine_Function::getThumbImage(@$tmp[0], 275, 145,null,true);
                $item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
                
            }
            unset($item);
            
            $count = count($objContent->getAllEnabledContentByCategory($cat['content_category_gid']));
            $this->setPagination($numRowPerPage, $currentPage, $count);                                                           
            $this->view->partner= $allPartner;
                                                   
        }
        
        $templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . ((@$cat['template'])?$cat['template']:'default');
        $templatePath .= '/index.' . Nine_Constant::VIEW_SUFFIX;
        
        $rootCatParent = $objContentCategory->getRootParent($categoryGid);
        
        $this->session->frontBanner = 'front/images/img/banner_04.jpg';
        $this->view->headTitle($cat['name']);
        
        $this->view->cat = $cat;
        
        /**
         * Render this template
         */
        $this->view->html = $this->view->render( $templatePath );
	}
	
	public function detailAction()
	{
		$objContent = new Models_Content();
        $objContentCategory = new Models_ContentCategory();
        
        $gid = $this->_getParam('id',false);
        
        if($gid == 229){
        	$this->view->menuId = 'materials';
        }
        $news = $objContent->setAllLanguages(true)->getContentById($gid);
        
       
        $this->view->news = $news;
		
	}
	
	public function lineAction()
	{
		$objContent = new Models_Content();
        $objContentCategory = new Models_ContentCategory();
        $this->setLayout("line");
        $gid = $this->_getParam('id',false);
        
        $this->view->menuId = $gid;
        $news = $objContent->setAllLanguages(true)->getContentById($gid);
        
       
        $this->view->news = $news;
		
	}
	
	private function _redirectToNotFoundPage()
	{
	    $this->_redirect("");
	}
}